package com.shxzz.shortlink.project.utils;

import cn.hutool.core.util.NumberUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author shxzz
 * @create 2023-12-20-20:48
 * @description 短链接工具类
 */
public class LinkUtil {
    /**
     * 一个月对应毫秒数
     */
    private static final long MAX_CACHE_EXPIRE_TIME = 30 * 24 * 60 * 60 * 1000L;

    private LinkUtil() {
    }

    /**
     * 获取短链接的缓存过期时间
     *
     * @param validDate 过期时间
     * @return 剩余有效时间（毫秒），上限为 MAX_CACHE_EXPIRE_TIME
     */
    public static long getCacheExpireTime(LocalDateTime validDate) {
        if (validDate == null) {
            return MAX_CACHE_EXPIRE_TIME;
        }
        long diffNow = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        long diffValidDate = validDate.toEpochSecond(ZoneOffset.of("+8"));
        return NumberUtil.min(MAX_CACHE_EXPIRE_TIME, 1000L * (diffValidDate - diffNow));
    }
}
